/*
 ============================================================================
 Page	     : 444-445
 Name        : manybook.c
 Date	     : 20210407
 Author      : Hbue 18060333
 Copyright   : Your copyright notice
 Description : 包含多本书的图书目录
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
char *s_gets(char *st, int n);
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100		/* 书籍的最大数量 */

struct book			/* 建立book模板 */
{
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

int main(void)
{
	struct book library[MAXBKS];		/* book类型结构的数组 */
	int count = 0;

	printf("Please enter the book title.\n");
	printf("Press [enter] at the start of a line to stop.\n");
	while(count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')
	{
		printf("Now enter the author.\n");
		s_gets(library[count].author, MAXAUTL);
		printf("Now enter the value.\n");
		scanf("%f", &library[count++].value);
		while(getchar() != '\n')
		{
			continue;		/* 清理输入行 */
		}
		if(count < MAXBKS)
		{
			printf("Enter the next title.\n");
		}
	}

	if(count > 0)
	{
		printf("Here is the list of your books:\n");
		for(int index = 0; index < count; index++)
		{
			printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
		}
	}
	else
	{
		printf("No books? Too bad.\n");
	}

	return 0;
}

char *s_gets(char *st, int n)
{
	char *ret_val = NULL;
	char *find = NULL;

	ret_val = fgets(st, n, stdin);
	if(ret_val)
	{
		find = strchr(st, '\n');		// 查找换行符
		if(find)				// 如果地址不是NULL,
		{
			*find = '\0';			// 在此处放置一个空字符
		}
		else
		{
			while(getchar() != '\n')
			{
				continue;		// 处理输入行中剩余的字符
			}
		}
	}

	return ret_val;
}

